package com.cskaoyan.service;

import com.cskaoyan.bean.*;
import com.cskaoyan.bean.common.BasePageInfo;
import com.cskaoyan.bean.common.CommonData;
import com.cskaoyan.mapper.MarketCouponMapper;
import com.cskaoyan.mapper.MarketCouponUserMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.ibatis.annotations.Param;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.*;

/***
 * title VxCouponServiceImpl
 * @description <TODO description class purpose>
 * @author ZCZ
 * @version 1.0.0
 * @create 2022/9/11 20:05
 **/
@Service
public class VxCouponServiceImpl implements VxCouponService {

    @Autowired
    MarketCouponMapper marketCouponMapper;
    @Autowired
    MarketCouponUserMapper marketCouponUserMapper;

    @Override
    public CommonData<MarketCoupon> myList(BasePageInfo info, Short status) {
        PageHelper.startPage(info.getPage(),info.getLimit());
        Subject subject = SecurityUtils.getSubject();
        //直接获取了user对象
        MarketUser user = (MarketUser)subject.getPrincipals().getPrimaryPrincipal();
        Integer userId = user.getId();
        //下面的是为了获取一个有couponId的list
        MarketCouponUserExample marketCouponUserExample = new MarketCouponUserExample();

        //判断优惠券是否过期，并且修改优惠券状态status
        List<MarketCouponUser> marketCouponUserList1=marketCouponUserMapper.selectByExample(marketCouponUserExample);
        for (MarketCouponUser marketCouponUser : marketCouponUserList1) {
            if(marketCouponUser.getEndTime()==null){
                marketCouponUser.setStatus((short)2);
                marketCouponUserMapper.updateByPrimaryKey(marketCouponUser);
            }else if(marketCouponUser.getUsedTime()!=null){
                marketCouponUser.setStatus((short)1);
                marketCouponUserMapper.updateByPrimaryKey(marketCouponUser);
        }
            else if(marketCouponUser.getEndTime().getTime()<new Date().getTime()){
                marketCouponUser.setStatus((short)2);
                marketCouponUserMapper.updateByPrimaryKey(marketCouponUser);
            }
        }
        MarketCouponUserExample.Criteria criteria1 = marketCouponUserExample.createCriteria();
        criteria1.andUserIdEqualTo(userId);
        if(status==0){
            criteria1.andStatusEqualTo(status);
        }
        if(status==1){
            criteria1.andStatusEqualTo(status);
        }
        if(status==2){
            criteria1.andStatusEqualTo(status);
        }
        List<MarketCouponUser> marketCouponUserList = marketCouponUserMapper.selectByExample(marketCouponUserExample);
        if(marketCouponUserList.size()==0){
            return null;
        }
        // 用list接收marketcouponuserId
        // Integer[] couponArray = new Integer[marketCouponUserList.size()] ;
        // int i=0;
        // for (MarketCouponUser marketCouponUser : marketCouponUserList) {
        //     couponArray[i]=marketCouponUser.getCouponId();
        //     i++;
        // }
        // List list = Arrays.asList(couponArray);
        MarketCouponExample marketCouponExample = new MarketCouponExample();
        marketCouponExample.setDistinct(true);
        MarketCouponExample.Criteria criteria = marketCouponExample.createCriteria();
        List<MarketCoupon> marketCouponList = new LinkedList<>();
        CommonData<MarketCoupon> marketCouponCommonData = new CommonData<>();
        for (MarketCouponUser marketCouponUser : marketCouponUserList) {
            MarketCoupon marketCoupon = marketCouponMapper.selectByPrimaryKey(marketCouponUser.getCouponId());
            if(marketCoupon.getStartTime()==null){
                marketCoupon.setStartTime(marketCouponUser.getStartTime());
                marketCoupon.setEndTime(marketCouponUser.getEndTime());
            }
            ((LinkedList<MarketCoupon>) marketCouponList).push(marketCoupon);
        }
        PageInfo pageInfo = new PageInfo(marketCouponList);
        Integer total = (int)pageInfo.getTotal();
        int pages = pageInfo.getPages();
        marketCouponCommonData.setTotal(total);
        marketCouponCommonData.setPages(pages);
        marketCouponCommonData.setLimit(info.getLimit());
        marketCouponCommonData.setPage(info.getPage());
        marketCouponCommonData.setList(marketCouponList);
        return marketCouponCommonData;
    }

    @Override
    public CommonData<MarketCoupon> list(BasePageInfo info) {
        PageHelper.startPage(info.getPage(),info.getLimit());


        MarketCouponExample marketCouponExample = new MarketCouponExample();

        //判断优惠券是否过期，并且修改优惠券状态status
        List<MarketCoupon> marketCouponList1=marketCouponMapper.selectByExample(marketCouponExample);
        for (MarketCoupon marketCoupon : marketCouponList1) {
            if(marketCoupon.getEndTime()==null){
                marketCoupon.setStatus((short)1);
                marketCouponMapper.updateByPrimaryKey(marketCoupon);
            }else if(marketCoupon.getEndTime().getTime()<new Date().getTime()){
                marketCoupon.setStatus((short)1);
                marketCouponMapper.updateByPrimaryKey(marketCoupon);
            }
        }

        marketCouponExample.setDistinct(true);
        MarketCouponExample.Criteria criteria = marketCouponExample.createCriteria();
        criteria.andDeletedEqualTo(false);
        criteria.andStatusEqualTo((short)0);
        List<MarketCoupon> marketCouponList = marketCouponMapper.selectByExample(marketCouponExample);

        CommonData<MarketCoupon> marketCouponCommonData = new CommonData<>();
        PageInfo pageInfo = new PageInfo(marketCouponList);
        Integer total = (int)pageInfo.getTotal();
        int pages = pageInfo.getPages();
        marketCouponCommonData.setTotal(total);
        marketCouponCommonData.setPages(pages);
        marketCouponCommonData.setList(marketCouponList);

        return marketCouponCommonData;

    }

    @Override
    public int receive(Map<String,Integer> map) {
        Integer couponId = map.get("couponId");
        Subject subject = SecurityUtils.getSubject();
        if(subject.getPrincipals()==null){
            return 804;
        }
        //直接获取了user对象
        MarketUser user = (MarketUser)subject.getPrincipals().getPrimaryPrincipal();
        Integer userId = user.getId();
        MarketCoupon marketCoupon = marketCouponMapper.selectByPrimaryKey(couponId);
        //为了判断是否达到持有上限
        MarketCouponUserExample example = new MarketCouponUserExample();
        MarketCouponUserExample.Criteria criteria = example.createCriteria();
        criteria.andUserIdEqualTo(userId).andCouponIdEqualTo(marketCoupon.getId());
        List<MarketCouponUser> marketCouponUserList = marketCouponUserMapper.selectByExample(example);
        if (marketCouponUserList!=null){
            int size = marketCouponUserList.size();
            if (size>=marketCoupon.getLimit()) {
                return 602;
            }
        }
        if(marketCoupon.getTotal()<=0){
            return 803;
        }
        if(marketCoupon.getType()==1){
            return 801;
        }
        if(marketCoupon.getType()==2){
            return 802;
        }
        Integer total =marketCoupon.getTotal()-1;
        marketCoupon.setTotal(total);
        marketCouponMapper.updateByPrimaryKey(marketCoupon);
        MarketCouponUser marketCouponUser = new MarketCouponUser(marketCoupon);
        marketCouponUser.setUserId(userId);
        marketCouponUser.setAddTime(new Date());
        marketCouponUser.setUpdateTime(new Date());


        //基于领取时间的有效天数来生成startTime和endTime
        if(marketCoupon.getTimeType()==0){
            Date date = new Date();
            marketCouponUser.setStartTime(date);
            int days = marketCoupon.getDays();
            Date date1 = new Date();
            date1.setTime(date.getTime() + days*1000*60*60*24);
            // date.setTime(date.getTime() + days*1000*60*60*24);
            marketCouponUser.setEndTime(date1);
        }
        marketCouponUserMapper.insert(marketCouponUser);
        return 800;
    }

    @Override
    public int exchange(Map<String,String> map) {
        Subject subject = SecurityUtils.getSubject();
        //直接获取了user对象
        MarketUser user = (MarketUser)subject.getPrincipals().getPrimaryPrincipal();
        Integer userId = user.getId();
        String code = map.get("code");
        MarketCoupon marketCoupon = marketCouponMapper.selectByCode(code);
        if (marketCoupon==null){
            return 601;
        }
        MarketCouponUserExample example = new MarketCouponUserExample();
        MarketCouponUserExample.Criteria criteria = example.createCriteria();
        criteria.andUserIdEqualTo(userId).andCouponIdEqualTo(marketCoupon.getId());
        List<MarketCouponUser> marketCouponUserList = marketCouponUserMapper.selectByExample(example);
        int size = marketCouponUserList.size();

        if (size>=marketCoupon.getLimit()) {
            return 602;
        }
        MarketCouponUser marketCouponUser = new MarketCouponUser(marketCoupon);
        marketCouponUser.setUserId(userId);
        marketCouponUser.setAddTime(new Date());
        marketCouponUser.setUpdateTime(new Date());

        //基于领取时间的有效天数来生成startTime和endTime
        if(marketCoupon.getTimeType()==0){
            Date date = new Date();
            marketCouponUser.setStartTime(date);
            int days = marketCoupon.getDays();
            Date date1 = new Date();
            date1.setTime(date.getTime() + days*1000*60*60*24);
            // date.setTime(date.getTime() + days*1000*60*60*24);
            marketCouponUser.setEndTime(date1);
        }
        marketCouponUserMapper.insert(marketCouponUser);
        return 200;
    }

    @Override
    public CommonData<MarketCoupon> selectList(int cartId) {
        PageHelper.startPage(1,10);
        Subject subject = SecurityUtils.getSubject();
        //直接获取了user对象
        MarketUser user = (MarketUser)subject.getPrincipals().getPrimaryPrincipal();
        Integer userId = user.getId();
        MarketCouponUserExample marketCouponUserExample = new MarketCouponUserExample();

        //判断优惠券是否过期，并且修改优惠券状态status
        List<MarketCouponUser> marketCouponUserList1=marketCouponUserMapper.selectByExample(marketCouponUserExample);
        for (MarketCouponUser marketCouponUser : marketCouponUserList1) {
            if(marketCouponUser.getEndTime()==null){
                marketCouponUser.setStatus((short)2);
                marketCouponUserMapper.updateByPrimaryKey(marketCouponUser);
            }else if(marketCouponUser.getUsedTime()!=null){
                marketCouponUser.setStatus((short)1);
                marketCouponUserMapper.updateByPrimaryKey(marketCouponUser);
                continue;
            }
            else if(marketCouponUser.getEndTime().getTime()<new Date().getTime()){
                marketCouponUser.setStatus((short)2);
                marketCouponUserMapper.updateByPrimaryKey(marketCouponUser);
            }
        }

        //-----------------------------------写到这里
        MarketCouponUserExample.Criteria criteria = marketCouponUserExample.createCriteria();
        criteria.andUserIdEqualTo(userId);
        criteria.andStatusEqualTo((short) 0);
        marketCouponUserExample.setDistinct(true);
        List<MarketCouponUser> marketCouponUserList = marketCouponUserMapper.selectByExample(marketCouponUserExample);
        if(marketCouponUserList==null){
            return null;
        }
        Integer[] couponArray = new Integer[marketCouponUserList.size()] ;
        int i=0;
        for (MarketCouponUser marketCouponUser : marketCouponUserList) {
            couponArray[i]=marketCouponUser.getCouponId();
            i++;
        }
        List list = Arrays.asList(couponArray);
        //根据couponList来取优惠券
        MarketCouponExample marketCouponExample = new MarketCouponExample();
        marketCouponExample.setDistinct(true);
        MarketCouponExample.Criteria criteria2 = marketCouponExample.createCriteria();
        criteria2.andIdIn(list);

        List<MarketCoupon> marketCouponList = marketCouponMapper.selectByExample(marketCouponExample);

        CommonData<MarketCoupon> marketCouponCommonData = new CommonData<>();
        PageInfo pageInfo = new PageInfo(marketCouponList);
        Integer total = (int)pageInfo.getTotal();
        int pages = pageInfo.getPages();
        marketCouponCommonData.setTotal(total);
        marketCouponCommonData.setPages(pages);
        marketCouponCommonData.setLimit(10);
        marketCouponCommonData.setPage(1);
        marketCouponCommonData.setList(marketCouponList);
        return marketCouponCommonData;

    }
}
